package com.hb.game.pk.ui.activity.game;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.hb.game.pk.R;
import com.hb.game.pk.adpter.SortDListAdapter;
import com.hb.game.pk.adpter.SortNormalListAdapter;
import com.hb.game.pk.api.Host;
import com.hb.game.pk.api.RedBag;
import com.hb.game.pk.base.BaseActivity;
import com.hb.game.pk.beans.SortDBean;
import com.hb.game.pk.beans.SortNormalBean;
import com.hb.game.pk.network.RetrofitCreateHelper;
import com.hb.game.pk.utils.ToastUtils;
import com.hb.game.pk.widgets.titlebar.TitleBar;
import com.google.gson.Gson;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class SortActivity extends BaseActivity {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.banner)
    ImageView banner;
    @BindView(R.id.rl_zt)
    RelativeLayout rlZt;
    @BindView(R.id.rl_fb)
    RelativeLayout rlFb;
    @BindView(R.id.rl_qb)
    RelativeLayout rlQb;
    @BindView(R.id.rl_yj)
    RelativeLayout rlYj;
    int type = 4;
    private Gson gson;
    List<SortDBean.DataBeanX.DataBean> sortDdata;
    List<SortNormalBean.DataBeanX.DataBean> sortNormalData;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sort;
    }

    @Override
    protected int getRootViewId() {
        return R.id.layout_root;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return R.id.rv_list;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        titleBar.getLlLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SortActivity.this.finish();
            }
        });
        gson = new Gson();
    }

    @Override
    protected void onRefreshData() {
        super.onRefreshData();
        Call<Object> renking = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).get_renking(type);
        renking.enqueue(new Callback<Object>() {
            @Override
            public void onResponse(Call<Object> call, Response<Object> response) {
                Object object = response.body();
                String body = gson.toJson(object);
                if (body == null) {
                    ToastUtils.showToast("请求错误");
                    return;
                }
                if (type == 4) {
//                    Map map = gson.fromJson(body, Map.class);
//                    LogUtils.d("map___" + map.toString());
                    SortDBean sortDBean = gson.fromJson(body, SortDBean.class);
                    if (sortDBean.getStatus() != 1) {
                        ToastUtils.showToast(sortDBean.getInfo());
                        return;
                    }
                    sortDdata = sortDBean.getData().getData();
                } else {
                    SortNormalBean sortNormalBean = gson.fromJson(body, SortNormalBean.class);
                    if (sortNormalBean.getStatus() != 1) {
                        ToastUtils.showToast(sortNormalBean.getInfo());
                        return;
                    }
                    sortNormalData = sortNormalBean.getData().getData();
                }
                initAdapter();

            }

            @Override
            public void onFailure(Call<Object> call, Throwable t) {
                t.printStackTrace();
                ToastUtils.showToast("网络请求错误");
            }
        });
    }

    @Override
    protected void initAdapter() {
        mAdapter = null;
        if (type == 4) {
            if (mAdapter == null) {
                sortDAdapter();
            } else {
                mAdapter.setNewData(sortDdata);
            }
        } else {
            if (mAdapter == null) {
                sortNormalAdapter();
            } else {
                mAdapter.setNewData(sortNormalData);
            }
        }
        super.initAdapter();
    }

    private void sortNormalAdapter() {
        mAdapter = new SortNormalListAdapter(R.layout.item_sort_list);
        mAdapter.setNewData(sortNormalData);
    }

    private void sortDAdapter() {
        mAdapter = new SortDListAdapter(R.layout.item_sort_list);
        mAdapter.setNewData(sortDdata);
    }

    @OnClick({R.id.rl_zt, R.id.rl_fb, R.id.rl_qb, R.id.rl_yj})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_zt:
                type = 4;
                showUp(rlZt, rlFb, rlQb, rlYj);
                break;
            case R.id.rl_fb:
                type = 1;
                showUp(rlFb, rlZt, rlQb, rlYj);
                break;
            case R.id.rl_qb:
                type = 2;
                showUp(rlQb, rlZt, rlFb, rlYj);
                break;
            case R.id.rl_yj:
                type = 3;
                showUp(rlYj, rlZt, rlFb, rlQb);
                break;
        }
        onRefreshData();
    }

    private void showUp(RelativeLayout clicked, RelativeLayout noClicked1, RelativeLayout noClicked2, RelativeLayout noClicked3) {
        clicked.getChildAt(1).setVisibility(View.VISIBLE);
        noClicked1.getChildAt(1).setVisibility(View.GONE);
        noClicked2.getChildAt(1).setVisibility(View.GONE);
        noClicked3.getChildAt(1).setVisibility(View.GONE);
    }

    @Override
    public boolean isNeedLoadMore() {
        return false;
    }
}
